package com.ruoyi.web.controller.common;

import com.google.code.kaptcha.Producer;
import com.ruoyi.common.constant.CacheConstants;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.sign.Base64;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.system.service.ISysConfigService;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Random;
import java.util.concurrent.TimeUnit;

@RestController
//@RequestMapping("/interaction")
@Api(tags = "数字验证码接口")
public class CapchaControllerNum {


    @Autowired
    private RedisCache redisCache;

    @Autowired
    private ISysConfigService configService;

    @GetMapping("/captchaImage")
    public AjaxResult getCode(HttpServletResponse response) throws IOException {
        AjaxResult ajax = AjaxResult.success();

        // 生成4位纯数字验证码
        String randomText = RandomStringUtils.randomNumeric(4); // Apache Commons Lang3

        // 生成验证码图片（使用原有图片生成逻辑）
        boolean captchaEnabled = configService.selectCaptchaEnabled();
        ajax.put("captchaEnabled", captchaEnabled);
        if (!captchaEnabled) {
            return ajax;
        }

        // 存储验证码到Redis（有效期2分钟）
        String uuid = IdUtils.simpleUUID();
        String verifyKey = CacheConstants.CAPTCHA_CODE_KEY + uuid;
        redisCache.setCacheObject(verifyKey, randomText, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);

        // 生成图片（使用纯数字专用样式）
        BufferedImage image = new BufferedImage(100, 36, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = image.createGraphics();
        try {
            // 设置背景和干扰线（简化版）
            g.setColor(Color.WHITE);
            g.fillRect(0, 0, 100, 36);
            g.setColor(new Color(0, 100, 200));
            g.setFont(new Font("Arial", Font.BOLD, 24));
            g.drawString(randomText, 20, 25);

            // 添加3条干扰线
            for (int i = 0; i < 3; i++) {
                int x1 = new Random().nextInt(100);
                int y1 = new Random().nextInt(36);
                int x2 = new Random().nextInt(100);
                int y2 = new Random().nextInt(36);
                g.drawLine(x1, y1, x2, y2);
            }
        } finally {
            g.dispose();
        }

        // 转为Base64返回
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        ImageIO.write(image, "jpg", os);
        ajax.put("img", Base64.encode(os.toByteArray()));
        ajax.put("uuid", uuid);

        return ajax;
    }
}
